﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ImageTagToolViewModel.Interfaces;

namespace ImageTagToolViewModel.Models
{
    /// <summary>
    /// This class contains information of an image.
    /// </summary>
    [Serializable]
    public class Picture : IMemoryUsageApproximable
    {
        /// <summary>
        /// Gets the full name of the image-file.
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// Gets the name of the image-file.
        /// </summary>
        public string Name
        {
            get
            {
                string[] parts = FileName.Split('\\');
                return parts[parts.Length - 1];
            }
        }

        /// <summary>
        /// The height of the image.
        /// </summary>
        public int Height { get; set; }

        /// <summary>
        /// The width of the image.
        /// </summary>
        public int Width { get; set; }

        /// <summary>
        /// The horizontal resolution of the image in DPI.
        /// </summary>
        public int HorizontalResolution { get; set; }

        /// <summary>
        /// The vertical resolution of the image in DPI.
        /// </summary>
        public int VerticalResolution { get; set; }

        /// <summary>
        /// The size of the image-file.
        /// </summary>
        public int FileSize { get; set; }

        /// <summary>
        /// How good the picture is.
        /// </summary>
        public byte Rating { get; set; }

        /// <summary>
        /// Tags concerning its scene, environment, etc.
        /// </summary>
        public List<string> SceneTags { get; set; }

        /// <summary>
        /// Tags concerning the people on the image.
        /// </summary>
        public List<string> PersonTags { get; set; }

        /// <summary>
        /// Tags concerning other data of the image.
        /// </summary>
        public List<string> OtherTags { get; set; }

        /// <summary>
        /// Creates a new picture instance.
        /// </summary>
        /// <param name="fileName">The full name of the image, including its file-path.</param>
        /// <param name="fileSize">Size of the image in bytes.</param>
        /// <param name="width">The width of the image in pixels.</param>
        /// <param name="height">The height of the image in pixels.</param>
        /// <param name="horizontalResolution">The horizontal resolution of the image in DPI.</param>
        /// <param name="verticalResolution">The vertical resolution of the image in DPI.</param>
        public Picture(string fileName, int fileSize, int width, int height, int horizontalResolution, int verticalResolution)
        {
            // Setting up static image data.
            FileName = fileName;
            FileSize = fileSize;
            Width = width;
            Height = height;
            HorizontalResolution = horizontalResolution;
            VerticalResolution = verticalResolution;

            // Setting up dynamic image data.
            Rating = 6;
            SceneTags = new List<string>();
            PersonTags = new List<string>();
            OtherTags = new List<string>();
        }

        public Picture()
        {

        }

        /// <summary>
        /// Tries to approximate the stored data in this Picture object.
        /// </summary>
        /// <returns>The amount of data in Bytes</returns>
        public int ApproximateMemoryUsage()
        {
            int memory = 0;
            memory += Encoding.UTF8.GetByteCount(FileName);
            memory += 4; // Height (4B)
            memory += 4; // Width (4B)
            memory += 4; // HorizontalResolution (4B)
            memory += 4; // VerticalResolution (4B)
            memory += 4; // FileSize (4B)
            memory += 1; // Rating (1B)
            
            foreach (var s in SceneTags)
                memory += Encoding.UTF8.GetByteCount(s);
            foreach (var s in PersonTags)
                memory += Encoding.UTF8.GetByteCount(s);
            foreach (var s in OtherTags)
                memory += Encoding.UTF8.GetByteCount(s);

            return memory;
        }
    }
}
